package com.zh.javaguide.算法;


import java.util.Arrays;

/**
 * Copyright (C) 广州中和互联网技术有限公司
 * 最⻓公共前缀
 *
 * @author 范家微 fanjiawei@borche.cn
 * @date :2021/1/27 21:12
 */
public class Demo1 {
	public static void main(String[] args) {
		String[] strings = {"flower", "flow", "flightt"};
		String[] strs = {null, null, "abc"};
		String[] strs2 = {};
		String[] strs3 = null;
		String[] strs4 = {"zhangsan"};


		System.out.println(getLongestPrefix(strs3));
		System.out.println(getLongestPrefix(strs));
		System.out.println(getLongestPrefix(strs2));
		System.out.println(getLongestPrefix(strings));
		System.out.println(getLongestPrefix(strs4));
	}


	public static String getLongestPrefix(String[] strs) {
		String result = "";
		if (chechStrs(strs)) {
			Arrays.sort(strs);
			int min = Math.min(strs[0].length(), strs[strs.length - 1].length());
			for (int i = 0; i < min; i++) {
				if (strs[0].charAt(i) == strs[strs.length - 1].charAt(i)) {
					result += strs[0].charAt(i);
				}

			}
		}
		return result;
	}



	private static boolean chechStrs(String[] strs) {
		boolean flag = false;
		if (strs != null) {
			// 遍历strs检查元素值
			for (int i = 0; i < strs.length; i++) {
				if (strs[i] != null && strs[i].length() != 0) {
					flag = true;
				} else {
					flag = false;
					break;
				}
			}
		}
		return flag;
	}
}

